{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6b873f4f",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://gdp.gotohui.com/\n",
      "https://deposit.gotohui.com/\n",
      "Scraping https://gdp.gotohui.com/\n",
      "Scraping https://deposit.gotohui.com/\n",
      "Data saved to scraped_data.xlsx\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "import pandas as pd\n",
    "\n",
    "# ① 从文本文件中读取网址\n",
    "def read_urls(file_path):\n",
    "    with open(file_path, 'r') as file:\n",
    "        urls = file.readlines()\n",
    "    # 去除每行末尾的换行符\n",
    "    urls = [url.strip() for url in urls]\n",
    "    return urls\n",
    "\n",
    "# ② 打印网址\n",
    "def print_urls(urls):\n",
    "    for url in urls:\n",
    "        print(url)\n",
    "\n",
    "# ③ 使用requests和BeautifulSoup获取并解析网页数据，并保存到Excel\n",
    "def scrape_and_save_to_excel(urls, excel_file):\n",
    "    data_dict = {}\n",
    "    for i, url in enumerate(urls, 1):\n",
    "        print(f\"Scraping {url}\")\n",
    "        response = requests.get(url)\n",
    "        if response.status_code == 200:\n",
    "            soup = BeautifulSoup(response.content, 'html.parser')\n",
    "            # 解析表格数据\n",
    "            table = soup.find('table')\n",
    "            if table:\n",
    "                data = []\n",
    "                rows = table.find_all('tr')\n",
    "                for row in rows:\n",
    "                    cells = row.find_all(['th', 'td'])\n",
    "                    row_data = [cell.get_text() for cell in cells]\n",
    "                    data.append(row_data)\n",
    "                sheet_name = f'Sheet_{i}'\n",
    "                data_dict[sheet_name] = data\n",
    "            else:\n",
    "                print(f\"No table found on {url}\")\n",
    "        else:\n",
    "            print(f\"Failed to scrape {url}\")\n",
    "\n",
    "    # 将数据保存到Excel的不同sheet中\n",
    "    with pd.ExcelWriter(excel_file) as writer:\n",
    "        for sheet_name, data in data_dict.items():\n",
    "            df = pd.DataFrame(data)\n",
    "            df.to_excel(writer, sheet_name=sheet_name, index=False)\n",
    "\n",
    "    print(\"Data saved to\", excel_file)\n",
    "\n",
    "# 主程序\n",
    "def main():\n",
    "    urls = read_urls('urls.txt')\n",
    "    print_urls(urls)\n",
    "    scrape_and_save_to_excel(urls, 'scraped_data.xlsx')\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "564a67ce",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d13f0445",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
